﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boggle
{
    public partial class Game
    {
        private class InProgressState : IGameState
        {
            private readonly Game game;

            public InProgressState(Game game)
            {
                if (game == null)
                {
                    throw new ArgumentNullException("game");
                }

                this.game = game;
            }

            #region IGameState Members

            public void Initialize()
            {
            }

            public void Start(DateTime startTime)
            {
                throw new InvalidOperationException("Cannot start a game that is already in progress");
            }

            public Boolean Next(DateTime currentTime)
            {
                if (currentTime < game.currentTime)
                {
                    throw new InvalidOperationException("The next time was earlier than the current time");
                }

                game.CompleteLookups();

                if (currentTime >= game.expectedEndTime)
                {
                    EndGame();
                }
                else
                {
                    game.currentTime = currentTime;
                    game.UpdateSecondsLeft();
                }

                return true;
            }

            public void SubmitWord()
            {
                if (String.IsNullOrEmpty(game.CurrentWord))
                {
                    return;
                }

                var word = game.CurrentWord.ToLower();
                game.CurrentWord = String.Empty;

                if (game.Words.Any(w => w.WordText == word))
                {
                    return;
                }

                var dice = game.grid.ContainsWord(word);

                var wordObject = new Word(word, dice);

                if (dice == null)
                {
                    wordObject.CurrentStatus = Word.Status.NotInGrid;
                    wordObject.Definition = "Not in grid.";
                }
                else
                {
                    wordObject.CurrentStatus = Word.Status.Validating;
					wordObject.Definition = "Looking up...";
                    game.DefineWord(wordObject);
                }

                game.Words.Add(wordObject);
                game.SelectedWord = wordObject;
            }

            public void EndGame()
            {
                game.SetState(GameState.Ended);
            }

            public void NewGame()
            {
                throw new InvalidOperationException("Cannot start a new game whilst one is in progress");
            }

            public IBoggleDictionary Dictionary
            {
                get { return game.dict; }
                set { throw new InvalidOperationException("Cannot set Dictionary while the game is in progress"); }
            }

            #endregion
        }
    }
}
